package cn.jinjimi.framework.router.impl;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import cn.jinjimi.app.AppContext;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.app.managers.NetRouteParamManager;
import cn.jinjimi.framework.router.RouterOptions;
import cn.jinjimi.app.bean.RouterService;

/**
 * Created by jinsen on 15/9/17.
 */
public class IntentRTask implements IRTask {

    private Intent mIntent;

    public IntentRTask(Intent it) {
        mIntent = it;
    }

    @Override
    public void open(RouterOptions option) {
        if (mIntent != null) {
            if (option.getBundle() != null) {

                Bundle bundle = option.getBundle();
                for (String key : bundle.keySet()) {
                    // 网页跳原生页面需一直携带的安全性参数值 2.6.2添加 --Roy
                    if (RouterService.ROUTER_PARAM.contains(key) && bundle.get(key) instanceof String) {
                        NetRouteParamManager.getInstance().addRouterParam(key, (String) bundle.get(key));
                    }
                }

                mIntent.putExtra(BundleKey.ORIGINAL_URL, option.getOriginUrl());
                mIntent.putExtras(bundle);

            }

            Context context = option.getContext();
            if (context == null) {
                context = AppContext.getInstance();
            }

            if (context != null) {
                if (!(context instanceof Activity)) {
                    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
                context.startActivity(mIntent);
            }
        }
    }
}